/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sales.managedbean;

import accountManagement.entity.Employee;
import accountManagement.entity.Salesman;
import accountManagement.sessionbean.SalesmanManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import sales.entity.SalesOrderEntity;
import sales.seesion.stateless.SalesOrderSessionBean;
import sales.util.exception.NoSalesOrderException;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ViewAllSalesOrderManagedBean {
private SalesOrderEntity selectedSalesOrder;

private Salesman employee;
@EJB
SalesOrderSessionBean salesOrderSessionBean;
@EJB
SalesmanManager employeeManager;

    /** Creates a new instance of ViewAllSalesOrderManagedBean */
    public ViewAllSalesOrderManagedBean() {
        selectedSalesOrder = new SalesOrderEntity();

    }
    
    public List<SalesOrderEntity> getSalesOrderList() throws IOException{
        List<SalesOrderEntity> result = new ArrayList();
        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long userId = (Long)request.getSession().getAttribute("userId");
        employee = employeeManager.getSalesmanById(userId);
        try { result = salesOrderSessionBean.getAllSalesOrders(employee);}
        catch (NoSalesOrderException e){
           FacesContext.getCurrentInstance().getExternalContext().redirect("error/error_noSalesOrder.xhtml");  
        }
        return result;
    }
    
    public void viewSalesOrder(SalesOrderEntity salesOrder) throws IOException{
       selectedSalesOrder = salesOrder;
       String lastURL = "viewAllSalesOrder.xhtml";
       FacesContext.getCurrentInstance().getExternalContext().getFlash().put("lastURL", lastURL);
       FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newSalesOrder", selectedSalesOrder);
       FacesContext.getCurrentInstance().getExternalContext().redirect("displaySalesOrderResult.xhtml");  
    }
 

    
//    public void amendSalesOrder(SalesOrderEntity salesOrder)throws IOException{
//       selectedSalesOrder = salesOrder; 
//       FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newSalesOrder", selectedSalesOrder);
//       FacesContext.getCurrentInstance().getExternalContext().redirect("amendPurchaseOrder.xhtml");  
//    }

    
    public SalesOrderEntity getSelectedSalesOrder() {
        return selectedSalesOrder;
    }

    public void setSelectedSalesOrder(SalesOrderEntity selectedSalesOrder) {
        this.selectedSalesOrder = selectedSalesOrder;
    }



    public Salesman getEmployee() {
        return employee;
    }

    public void setEmployee(Salesman employee) {
        this.employee = employee;
    }


}
